<?php

namespace app\admin\controller\team;

use app\common\controller\Backend;
use think\Db;
use ReflectionFunction;
class Index extends Backend
{

    /**
     * Index模型对象
     * @var \app\admin\model\team\Index
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\team\Index;

    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

//            $list = model('user')
//                ->where('status',1)
//                ->order('createtime desc')
//                ->paginate($limit);

//            foreach ($list as $row) {
//                $row->visible(['id','type','status','money','content','create_time']);
//                $row->visible(['user']);
//                $row->getRelation('user')->visible(['nickname','avatar']);
//            }
            $func = new ReflectionFunction($where);
            $where = $func->getStaticVariables();
            
            if($where['where'])
            {
                foreach($where['where'] as &$val)
                {
                    if($val[0] == "index.create_time")
                    {
                        $val[0] = 'create_time';
                        $status = 1;
                        $start  = $val[2][0];
                        $end    = $val[2][1];
                    }else{
                        $status = 0;
                    }
                }
                
            }else{
                $status = 0;
            }
            
            $list = Db::name('user')
                ->where('status',1)
                ->field('id,nickname,avatar,createtime')
                ->order('createtime desc')
                ->paginate($limit)->toArray();


            foreach($list['data'] as &$val)
            {
                if($status == 1)
                {
                    $val['revenue'] = Db::name('user_consume')->where(['user_id'=>$val['id'],'type'=>3])->where('sub_user_id','>=',0)->where('create_time','between',[strtotime($start),strtotime($end)])->sum('money');
                    $create_time = Db::name('user_consume')->where(['user_id'=>$val['id'],'type'=>3])->where('sub_user_id','>',0)->where('create_time','between',[strtotime($start),strtotime($end)])->value('create_time');
                }else{
                    $val['revenue'] = Db::name('user_consume')->where(['user_id'=>$val['id'],'type'=>3])->where('sub_user_id','>=',0)->sum('money');
                    $create_time = Db::name('user_consume')->where(['user_id'=>$val['id'],'type'=>3])->where('sub_user_id','>',0)->value('create_time');
                }
                
                if($create_time)
                {
                    $val['create_time'] = date('Y-m-d H:i:s',$create_time);
                }else{
                    $val['create_time'] = date('Y-m-d H:i:s',time());
                }
            }


            if($status == 1){
                // 总金额
                $money  = Db::name('user')
                    ->alias('u')
                    ->join('user_consume uc','uc.user_id = u.id')
                    ->where(['u.status'=>1,'uc.type'=>3])
                    ->where('uc.sub_user_id','>',0)
                    ->where('uc.create_time','between',[strtotime($start),strtotime($end)])
                    ->sum('uc.money');
            }else{
                // 总金额
                $money  = Db::name('user')
                    ->alias('u')
                    ->join('user_consume uc','uc.user_id = u.id')
                    ->where(['u.status'=>1,'uc.type'=>3])
                    ->where('uc.sub_user_id','>',0)
                    ->sum('uc.money');
            }

            $useTimes = array_column($list['data'], 'revenue');
            // 排序，降序：SORT_DESC，升序：SORT_ASC
            array_multisort($useTimes,SORT_DESC,$list['data']);
            
            $result = array("total" => $list['total'], "rows" => $list['data'],"extend" => ['money' => $money]);

            return json($result);
        }
        return $this->view->fetch();
    }


    /*
     * 我的团队
     * */
    public function team($ids)
    {
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list = Db::name('user')
            ->where(['pid'=>['like','%'. $ids .'%']])
            ->field('id,nickname,avatar,createtime')
            ->paginate(10)->each(function($item,$key){
                $item['createtime'] = date('Y-m-d H:i',$item['createtime']);
                return $item;
            });

//        if($list)
//        {
//            foreach($list as &$val)
//            {
//                $val['createtime'] = date('Y-m-d H:i',$val['createtime']);
//            }
//
//        }

        $this->assign('row',$list);
//        $this->assign('page',$page);
        return $this->view->fetch();
    }


    /*
     * 下级
     * */
    public function subordinate()
    {
        $id = $this->request->param('id');
        
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list = Db::name('user')
            ->where(['pid'=>['like','%'. $id .'%']])
            ->field('id,nickname,avatar,createtime')
            ->order('createtime desc')
            ->select();


        $page =Db::name('user')
            ->where(['pid'=>['like','%'. $id .'%']])
            ->paginate($limit);

        if($list)
        {
            foreach($list as &$val)
            {
                $val['createtime'] = date('Y-m-d H:i',$val['createtime']);
            }

        }

        $this->assign('row',$list);
        $this->assign('page',$page);
        return $this->view->fetch();
    }


}